﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Rison.Web.Common;
using Rison.BusinessFacade.Common;
using Rison.BusinessFacade.Helper;
using Rison.BusinessFacade.Entity.Biz;
using Rison.BusinessFacade.Facade.Biz;

namespace Rison.Web.admin
{
    public partial class InfomationEdit : PageBase
    {

        #region "  page init  "
        protected void Page_Init(object sender, EventArgs e)
        {
            this.FileUpload1.AttachmentTablename = "Biz_InfomationAtt";
            this.FileUpload1.UploadFolder = "\\File\\Upload\\info\\" + CurrentLoginedUser.UserId;
        }
        #endregion


        #region "  page load  "
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(labMsg.Text)) labMsg.Text = string.Empty;

            if (!this.IsPostBack)
            {
                if (base.IsEdit)
                {
                    LoadData();                    
                }
            }
        }
        #endregion


        #region "  load data  "
        private void LoadData()
        {
            InfomationFacade facade = new InfomationFacade();
            Infomation objInfo = facade.Retrieve(base.key, true) as Infomation;

            if (objInfo != null)
            {
                txtSubject.Text = objInfo.Subject;
                txtBody.Value = objInfo.Content;
            }

            facade = null; objInfo = null;
        }
        #endregion
        


        #region "  button events  "
        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (ValidateData())
                SaveData();
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            Response.Redirect(Request.RawUrl);
        }

        protected void btnBack_Click(object sender, EventArgs e)
        {
            Response.Redirect("InfosView.aspx");
        }
        #endregion


        #region "  save data  "
        private bool ValidateData()
        {
            return string.IsNullOrEmpty(labMsg.Text);
        }

        private void SaveData()
        {
            InfomationFacade facade = new InfomationFacade();
            Infomation objInfo = null;
            try
            {
                if (base.IsEdit)
                    objInfo = facade.Retrieve(base.key, true) as Infomation;
                else
                {
                    objInfo = new Infomation();
                    objInfo.Creator = CurrentLoginedUser.UID;
                    objInfo.Category = Convert.ToInt32(Request.QueryString["category"]);
                }

                objInfo.Subject = txtSubject.Text;
                objInfo.Content = txtBody.Value;

                long returnValue = 0;
                if (base.IsEdit)
                {
                    returnValue = facade.Update(objInfo, getAttachments());
                }
                else
                {
                    returnValue = facade.Save(objInfo, getAttachments());
                }

                if (returnValue >= 1)
                    Response.Redirect("InfosView.aspx");
                else
                    labMsg.Text = "保存错误，请重试！";
            }
            catch (Exception ex) { labMsg.Text = "保存错误，请检查输入的数据或重试！" + ex.Message; }
            finally { facade = null; objInfo = null; }
        }
        #endregion


        #region "  如果上传了图片，则直接在信息框中显示  "
        private string ImgAttachments
        {
            get { return (ViewState["ImgAttachmentNum"] == null ? string.Empty : ViewState["ImgAttachmentNum"].ToString()); }
            set { ViewState["ImgAttachmentNum"] = value; }
        }

        protected void Page_PreRender(object sender, EventArgs e)
        {
            if (this.FileUpload1.Attachments.Count > 0)
            {
                List<SysAttachment> attachments = this.FileUpload1.Attachments;
                if (attachments != null && attachments.Count > 0)
                {
                    foreach (SysAttachment att in attachments)
                    {
                        if (".jpg".Equals(att.Extension) || ".gif".Equals(att.Extension) || ".bmp".Equals(att.Extension))
                        {
                            if (this.txtBody.Value.IndexOf(att.FilePath) < 0)
                            {
                                if (ImgAttachments.IndexOf(att.FilePath) < 0)
                                {
                                    ImgAttachments += att.FilePath;
                                    this.txtBody.Value += "<img alt=\"\" src=\"" + base.UrlBase2 + att.FilePath + "\" />";
                                }
                            }
                        }
                    }
                }

                attachments = null;
            }
        }
        #endregion


        #region "  取得上传的附近（不包括图片）  "
        private List<SysAttachment> getAttachments()
        {
            List<SysAttachment> attachments = null;

            if (this.FileUpload1.Attachments.Count > 0)
            {
                attachments = this.FileUpload1.Attachments;

                if (attachments != null && attachments.Count > 0)
                {
                    for (int i = 0; i < attachments.Count; i++)
                    {
                        //图片不作为附件保存，客户端不能下载
                        if (".jpg".Equals(attachments[i].Extension) || ".gif".Equals(attachments[i].Extension) || ".bmp".Equals(attachments[i].Extension))
                        {
                            attachments.RemoveAt(i);
                            i = i - 1;
                        }

                        if (i == attachments.Count - 1)
                            break;
                    }
                }
            }

            return attachments;
        }
        #endregion


    }
}
